package com.dl.mall.service.admin.impl;

import cn.dev33.satoken.secure.SaSecureUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dl.mall.entity.Page;
import com.dl.mall.entity.admin.Admin;
import com.dl.mall.mapper.AdminMapper;
import com.dl.mall.mapper.UserMapper;
import com.dl.mall.service.admin.AdminService;
import com.dl.mall.utils.PageUtils;
import com.dl.mall.utils.UUIDUtils;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author DL
 * @create 2022-03-29 15:43
 */
@Service
@Transactional
public class AdminServiceImpl implements AdminService {

    @Resource
    private AdminMapper adminMapper;


    @Override
    public boolean addAdmin(JSONObject jsonObject) {
        String userId = UUIDUtils.getUUID10();
        JSONObject addAdminForm = jsonObject.getJSONObject("addAdminForm");
        String username = addAdminForm.getString("username");
        String password = addAdminForm.getString("password");
        String md5BySalt = SaSecureUtil.md5BySalt(password, userId);
        addAdminForm.put("userId", userId);
        addAdminForm.put("password", md5BySalt);
        return adminMapper.insertAdmin(addAdminForm) != 0;
    }

    @Override
    public Map<String, String> getRoleById(String userId) {
        return adminMapper.queryRoleById(userId);
    }

    @Override
    public Admin login(String username) {
        return adminMapper.queryAdminByUsername(username);
    }

    @Override
    public Admin getAdminByUsername(String username) {
        return adminMapper.queryAdminByUsername(username);
    }


    /* ===========admin======== */

    @Override
    public Admin getAdminInfoById(String userId) {
        return adminMapper.queryAdminInfoById(userId);
    }

    @Override
    public boolean modifyAdminRole(String userId, JSONArray roleIdList) {
        System.out.println("roleIdList = " + roleIdList);
        int delete = adminMapper.deleteAdminRoleById(userId);
        int insert = adminMapper.insertAdminRole(userId, roleIdList);
        return delete != 0 && insert != 0;
    }

    @Override
    public String resetPassword(String userId) {
        String md5BySalt = SaSecureUtil.md5BySalt("123", userId);

        if (adminMapper.updateAdminPasswordByReset(userId, md5BySalt) != 0){
            return md5BySalt;
        }
        return null;
    }

    @Override
    public boolean modifyPassword(String userId, String newPassword) {
        String md5BySalt = SaSecureUtil.md5BySalt(newPassword, userId);
        return adminMapper.updateAdminPassword(userId, md5BySalt) != 0;
    }

    @Override
    public boolean modifyAdminInfo(JSONObject jsonObject) {
        JSONObject modifyAllInfoForm = jsonObject.getJSONObject("modifyAllInfoForm");
        return adminMapper.updateAdminInfo(modifyAllInfoForm) != 0;
    }

    @Override
    public boolean modifyAdminState(String userId, Integer state) {
        return adminMapper.updateAdminState(userId, state) != 0;
    }

    /**
     * 根据 条件 查询用户列表
     * @param condition 条件
     * @return 用户分页
     */
    @Override
    public Page<Admin> getAdminByCondition(JSONObject condition) {
        PageHelper.startPage(condition.getInteger("currentPage"), 10);

        List<Admin> adminList = adminMapper.queryAdminByCondition(condition);

        return (Page<Admin>) PageUtils.getPage(adminList);
    }

}
